using System;
using System.Diagnostics;
using System.Globalization;

namespace Atomic.Libraries.Physics.Units
{
	[Serializable]
	[DebuggerDisplay("{DebuggerDisplay}")]
	public class Length : IQuantity
	{
		private double value;

		private Length(double value)
		{
			this.value = value;
		}

		public static Length FromUnit(IUnitSystem system, double value)
		{
			if (system.LengthScaling == 1.0)
			{
				return new Length(value);
			}
			else
			{
				return new Length(value / system.LengthScaling);
			}
		}

		public static Length FromAngstrom(double value)
		{
			return FromUnit(UnitSystem.AtomicSystem, value);
		}

		public static Length FromMeter(double value)
		{
			return FromUnit(UnitSystem.InternationalSystem, value);
		}

		public static Length FromNanoMeter(double value)
		{
			return FromMeter(value * 1.0e-9);
		}
		
		public double ToUnit(IUnitSystem system)
		{
			if (system.LengthScaling == 1.0)
			{
				return value;
			}
			else
			{
				return value * system.LengthScaling;
			}
		}

		public double ToAngstrom()
		{
			return ToUnit(UnitSystem.AtomicSystem);
		}

		public double ToMeter()
		{
			return ToUnit(UnitSystem.InternationalSystem);
		}

		public double ToNanoMeter()
		{
			return ToMeter() * 1.0e9;
		}

		public static implicit operator double(Length value)
		{
			return value.ToUnit(UnitSystem.AtomicSystem);
		}

		public static implicit operator Length(double value)
		{
			return FromUnit(UnitSystem.AtomicSystem, value);
		}

		private string DebuggerDisplay
		{
			get
			{
				return string.Format(CultureInfo.InvariantCulture, "{0} Å = {1} m", ToAngstrom(), ToMeter());
			}
		}
	}
}
